home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: Tough FACTORIAL math problem...
- Date: 16 Feb 1996 23:17:18 GMT
- Organization: Microsoft Corporation
- Message-ID: <4g339u$dkk@news.microsoft.com>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fv16m$cuj@winx03.informatik.uni-wuerzburg.de>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <4fv16m$cuj@winx03.informatik.uni-wuerzburg.de>, schoof@informatik.uni-wuerzburg.de says...
- { Snip }
- >int factorial_lsd(int n)
- >{
- > int result=1, i;
- >
- > if(n>1)
- > for(i=1;i<=n;i++)
- > {
- > result*=i;
- > while(result%10==0) result/=10;
- > result%=100;
- > }
- > return temp%10;
- >}
- >
-
- <Dik.Winter@cwi.nl> has shown me the error of my
- ways, and I repent in dust and ashes ;>)
-
- Jochen's program is almost there, but it will fail
- when the factor produces two zeros. Here is one
- that will do the job. The number "100000L" has to
- be chosen carefully, so that it will not overflow
- when multiplied by the next factor, and so that it
- will not truncated needed digits of precision. It
- must be larger than n and smaller than MAX_LONG/n:
-
- long factorial_lsd( long n )
- {
- long result=1, i, count;
-
- if ( n > 1 )
- for ( i=1; i<=n; i++ )
- {
- result *= i;
- count = 0;
- while( result%10 == 0 )
- {
- result /= 10;
- count++;
- }
- result %= 100000L;
- }
- return result%10;
- }
- #define TEST
- #ifdef TEST
-
- #include <stdio.h>
- #include <stdlib.h>
- /****************************************************************
- 100!
- 933 26215 44394 41526 81699 23885 62667 00490 71596 82643
- 81621 46859 29638 95217 59999 32299 15608 94146 39761 56518
- 28625 36979 20827 22375 82511 85210 91686 40000 00000 00000
- 00000 00000
-
- 200!
- 78865 78673 64790 50355 23632 13932 18506 22951 35977 68717
- 32632 94742 53324 43594 49963 40334 29203 04284 01198 46239
- 04177 21213 89196 38830 25764 27902 42637 10506 19266 24952
- 82993 11134 62857 27076 33172 37396 98894 39224 45621 45166
- 42402 54033 29186 41312 27428 29485 32775 24242 40757 39032
- 40321 25740 55795 68660 22603 19041 70324 06235 17008 58796
- 17892 22227 89623 70389 73747 20000 00000 00000 00000 00000
- 00000 00000 00000 00000 00000
-
- 300!
- 30605 75122 16440 63603 53704 61297 26862 93885 88804 17357
- 69994 16776 74125 94765 33176 71686 74655 15291 42247 75733
- 49939 14788 87017 26368 86426 39077 59003 15422 68429 27906
- 97455 98412 25476 93027 19546 04008 01221 57762 52176 85425
- 59653 56903 50678 87252 64321 89626 42993 65204 57644 88303
- 88909 75394 34896 25436 05322 59807 76521 27082 24376 39449
- 12012 86786 75368 30571 22936 81943 64995 64604 98166 45022
- 77165 00185 17654 64693 40112 22603 47297 24066 33325 85835
- 06870 15016 97941 68850 35375 21375 54910 28912 64071 57154
- 83028 22849 37952 63658 01452 35233 15693 64822 33436 79925
- 45940 95276 82060 80622 32812 38738 38808 17049 60000 00000
- 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000
- 00000 00000 00000
-
- 400!
- 6403 45228 46623 89526 23479 70319 50300 58507 02583 02600
- 29594 58684 44594 28023 97169 18683 14362 78478 64746 32646
- 76294 35057 50358 56810 84829 81628 83517 43522 89619 88646
- 80299 79373 41654 15083 81624 26461 94235 23070 46244 32501
- 51144 48670 89066 27739 14918 11733 19559 96440 70954 96713
- 45290 47702 03224 34911 21079 75932 80795 10154 53726 67251
- 62787 78900 09349 76376 57103 26350 33153 39653 49868 38683
- 13393 52024 37378 81577 86791 50631 18587 02618 27016 98197
- 40062 98302 53085 91298 34616 22723 04558 33952 07596 11505
- 30223 60868 10433 29725 51948 52674 43223 24386 69948 42240
- 42325 99805 55161 06359 42376 96139 92319 17134 06385 89965
- 37970 14782 72066 06320 21737 94720 10321 35662 46138 09077
- 94230 45973 60699 56759 58360 96158 71512 99138 22286 57857
- 95493 61617 65448 04532 22007 82581 84008 48436 41559 12294
- 54275 38480 35583 74518 02267 59000 61399 56014 55952 06127
- 21119 29181 05032 49100 80000 00000 00000 00000 00000 00000
- 00000 00000 00000 00000 00000 00000 00000 00000 00000 00000
- 00000 00000 00000 00000
- ***************************************************************/
-
- int main()
- {
- printf("last digit of 100! = %ld. Should be '4'\n", factorial_lsd( 100 ));
- printf("last digit of 200! = %ld. Should be '2'\n", factorial_lsd( 200 ));
- printf("last digit of 300! = %ld. Should be '6'\n", factorial_lsd( 300 ));
- printf("last digit of 400! = %ld. Should be '8'\n", factorial_lsd( 400 ));
- return 0;
- }
- #endif
- /* On my machine, I get:
- last digit of 100! = 4. Should be '4'
- last digit of 200! = 2. Should be '2'
- last digit of 300! = 6. Should be '6'
- last digit of 400! = 8. Should be '8'
- **************************************/
-
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-